/**
 * @Author houlijx
 * @Description //TODO $
 * @Date 2021/8/26 下午10:51
 **/
package stringUtils

import (
	"encoding/json"
	"math/rand"
	"time"
	"unsafe"
)

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

const (
	letterIdxBits = 6                    // 6 bits to represent a letter index
	letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
	letterIdxMax  = 63 / letterIdxBits   // # of letter indices fitting in 63 bits
)

var src = rand.NewSource(time.Now().UnixNano())

func IsEmpty(text string) bool  {
	return text == ""
}

func IsContain(items []string, item string) bool  {
	for _, eachItem := range items {
		if eachItem == item {
			return true
		}
	}
	return false
}

//RandomString
//随机字符数
//
func RandomString(n int) string {
	b := make([]byte, n)
	for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
		if remain == 0 {
			cache, remain = src.Int63(), letterIdxMax
		}
		if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
			b[i] = letterBytes[idx]
			i--
		}
		cache >>= letterIdxBits
		remain--
	}

	return *(*string)(unsafe.Pointer(&b))
}

func ConvertStruct(obj interface{}) string {
	result, err := json.Marshal(&obj)
	if err != nil {
		return ""
	}
	return string(result)
}